﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using LegSec.NugetThirdPartyLicenseFileGenerator;
using NDesk.Options;
using log4net;

namespace LegSec.Cli
{
    public class LegSecCli
    {

        private static readonly ILog LOG = LogManager.GetLogger(typeof(Program));
        private static string GeneratedFileName = "ThirdPartyLicenses.xml";
        private static string RootDirectory =
            Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.LastIndexOf('\\'));
        private static string GeneratedFileLocation = RootDirectory;
        private static string IgnoredNameSpace = "";
        OptionSet commandLineOptions = new OptionSet()
                               {
                                   {"r=", "Root ", parameter => RootDirectory = parameter},
                                   {"o=", "Output location", parameter => GeneratedFileLocation = parameter},
                                   {"n=", "Output name", parameter => GeneratedFileName = parameter},
                                   {"i=", "ignore me", parameter => IgnoredNameSpace = parameter}
                               };

        public LegSecCli(String[] args)
        {
            var extra = commandLineOptions.Parse(args);
            foreach (var VARIABLE in extra)
            {
                LOG.Info(String.Format("Extra???????? [{0}]", VARIABLE));
            }
            LOG.Info(String.Format("Looking for Nupkg files in root directory [{0}]", RootDirectory));
            LOG.Info(String.Format("Output location set to [{0}]", GeneratedFileLocation));
            LOG.Info(String.Format("Output file set to [{0}]", GeneratedFileName));
            LOG.Info(String.Format("Ignored namespace set to [{0}]", IgnoredNameSpace));
        }

        public void Run()
        {
            var licenseGenerator = new LicenseFileGenerator(RootDirectory, GeneratedFileLocation, GeneratedFileName, IgnoredNameSpace);
            licenseGenerator.Generate();            
        }
    }
}
